package com.eptok.gw.szcups.web.servlet;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ys8583.parse.IOMessage;

import com.eptok.gw.szcups.pojo.BkCardBncQry;
import com.eptok.gw.szcups.pojo.CancelCardPay;
import com.eptok.gw.szcups.pojo.CardPay;
import com.eptok.gw.szcups.pojo.CtCardRepayment;
import com.eptok.gw.szcups.pojo.Relationship;
import com.eptok.gw.szcups.pojo.Reversal;
import com.eptok.gw.szcups.pojo.SigleColt;
import com.eptok.gw.szcups.pojo.XmlBaseMessage;
import com.eptok.gw.szcups.pojo.XmlMessage;
import com.eptok.gw.szcups.service.BaseService;
import com.eptok.gw.szcups.service.CancelCardPayService;
import com.eptok.gw.szcups.service.CardPayService;
import com.eptok.gw.szcups.service.CardQueryService;
import com.eptok.gw.szcups.service.CtCardRepaymentService;
import com.eptok.gw.szcups.service.NotSupportService;
import com.eptok.gw.szcups.service.RelationshipService;
import com.eptok.gw.szcups.service.ReturnCardPayService;
import com.eptok.gw.szcups.service.ReversalService;
import com.eptok.gw.szcups.service.SingleCollectingService;

public class SzcupsServlet extends HttpServlet {

	
	private static final Logger log = LoggerFactory.getLogger(SzcupsServlet.class);

	/**
	 * Constructor of the object.
	 */
	public SzcupsServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String msg = request.getParameter("Msg");
		log.info("recv from "+request.getRemoteAddr()+" : "+msg);
		byte[] msgbytes = Base64.decodeBase64(msg);
		log.info("Base64 decode "+request.getRemoteAddr()+" : "+new String(msgbytes,"GBK"));
		SAXReader saxReader = new SAXReader();
		Document document = null;
		try {
			document = saxReader.read(new ByteArrayInputStream(msgbytes),"GBK");
		} catch (DocumentException e) {
			log.error("读取XML出错",e);
			return;
		}
		XmlBaseMessage xmlBaseMessage = new XmlBaseMessage();
		try {
			xmlBaseMessage.decode(document);
		} catch (Exception e) {
			log.error("报文格式错误",e);
			return;
		}
		log.debug("接收到的报文类型:"+xmlBaseMessage.getMsgCode());
		BaseService baseService;
		XmlMessage xmlMessage;
		if(xmlBaseMessage.getMsgCode().equals("S1001")){
			xmlMessage = new Relationship();
			baseService = new RelationshipService();
		}else if(xmlBaseMessage.getMsgCode().equals("S2001")){
			xmlMessage = new BkCardBncQry();
			baseService = new CardQueryService();
		}else if(xmlBaseMessage.getMsgCode().equals("S3001")){
			xmlMessage = new CardPay();
			baseService = new CardPayService();
		}else if(xmlBaseMessage.getMsgCode().equals("S3002")){
			xmlMessage = new CancelCardPay();
			baseService = new CancelCardPayService();
		}else if(xmlBaseMessage.getMsgCode().equals("S3003")){
			xmlMessage = new CancelCardPay();
			baseService = new ReturnCardPayService();
		}else if(xmlBaseMessage.getMsgCode().equals("S3004")){
			xmlMessage = new CtCardRepayment();
			baseService = new CtCardRepaymentService();
		}else if(xmlBaseMessage.getMsgCode().equals("S3005")){
			xmlMessage = new SigleColt();
			baseService = new SingleCollectingService();
		}else if(xmlBaseMessage.getMsgCode().equals("S4001")){
			xmlMessage = new Reversal();
			baseService = new ReversalService();
		}else{
			xmlMessage = new XmlBaseMessage();
			baseService = new NotSupportService();
		}
		String str = "";
		try {
			xmlMessage.decode(document);
			IOMessage iomessage = baseService.process(xmlMessage);
			str = xmlMessage.encode(iomessage);
		} catch (Exception e) {
			log.error("系统异常",e);
			xmlBaseMessage.setStatus("ZZ");
			try {
				str = xmlBaseMessage.encode(null);
			} catch (Exception e1) {
				log.error("系统异常",e1);
			}
		}
		String base64Str = Base64.encodeBase64String(str.getBytes("GBK"));
		log.info("ready send to "+request.getRemoteAddr()+" : "+str);
		response.getWriter().write(base64Str);
		log.info("send to "+request.getRemoteAddr()+" : "+base64Str);
	}
	

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
